package com.zlm.zy.zhoujuanjuan.appsecurity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.lib.security.VsckManager;
import com.zlm.zy.zhoujuanjuan.a.EncodeUtils;
import com.zlm.zy.zhoujuanjuan.a.RSAEncrypt;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = "Main";

  // 签名md5
  //private static final String signStr = "EA9BE293BEA43FD2C5F6603B879FF361"; //A+
  //private static final String signStr = "E59FEF35A45B72A6ABFE7DAD6FAD746E"; //本工程

  //private static final String signStr = "6226AA6721E4401701497E9902D66A57"; // anchang
  private static final String signStr = "075F24531A3B15544913F5188C311E0D"; // fjh
  //private RSAEncrypt rsaEncrypt = RSAEncrypt.getInstance();
  private static final String filePath = "file://android_asset/aplus.apk";

  //static {
  //  System.loadLibrary("vsck");
  //}

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv = (TextView) findViewById(R.id.sample_text);
    //checkPwdEnc("");
    //for(int i=0 ; i<20;i++){
    //  try {
    //    Thread th = new Thread(new Runnable() {
    //      @Override
    //      public void run() {
    //        checkLoginEnc(null);
    //      }
    //    });
    //    th.start();
    //    th.sleep(1000);
    //  } catch (InterruptedException e) {
    //    e.printStackTrace();
    //  }
    //}

    checkSign();
  }

  /**
   * 签名校验
   */
  private void checkSign() {
    //VsckManager.loadVSFromC(getApplicationContext(), 0);
    //Log.e(TAG,"----main get md5----"+VsckManager.getbbCourseKeyFromC(getApplicationContext()));
    //boolean b = VsckManager.loadVSFromC(getApplicationContext());

    //Toast.makeText(this, b ? "签名一致" : "签名不一致", Toast.LENGTH_LONG).show();

    try {
      //用私钥对签名文件md5进行加密，然后进行md5编码得到security.c中的SING_KEY
      byte[] md5Str = RSAEncrypt.encrypt(RSAEncrypt.getPrivateKey(), signStr.getBytes());
      //Base64加密  该字符需要写入comment中
      String md5StrBase64Enc = EncodeUtils.base64Encode(md5Str);
      Log.e(TAG, "----md5StrBase64Enc----" + md5StrBase64Enc);
      //Base64解密
      byte[] md5StrBase64Dec = EncodeUtils.base64Decode(md5StrBase64Enc);
      //公钥解密  这里之前不能转换成String  转换成String再getByte后获取的数据类型不一致 详情可看源码
      //byte[] deStr = RSAEncrypt.decrypt(RSAEncrypt.getPublicKey(), md5StrBase64Dec);
      //Log.e(TAG, "----deStr----" + new String(deStr));

      //加密完写入comment
      //CommentUtil.setZipComment(CommentUtil.getPackagePath(this), md5Str.toString());
      //获取comment内容
      //String commonStr = CommentUtil.getZipComment(filePath);
      //Log.e(TAG, "----commonStr----" + commonStr);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 本地存储加密
   */
  private void checkPwdEnc(String pwd) {
    Log.d(TAG, "------pwd init------" + pwd);
    String encPwd = VsckManager.encP2local(pwd);
    Log.d(TAG, "------pwd enc------" + encPwd);
    String decPwd = VsckManager.decLocal2P(encPwd);
    Log.d(TAG, "------pwd dec------" + decPwd);
  }

  /**
   * 登录加密
   */
  private void checkLoginEnc(String pwd) {
    Log.d(TAG, "------pwd login------" + pwd);
    String encPwd = VsckManager.encLP2Net(pwd);
    //Log.d(TAG, "-----final pwd------" + encPwd);
    //Log.d(TAG,"----+++++= pwd++++------"+EncodeUtils.base64Decode(encPwd));
  }
}
